﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Diagnostics.Eventing.Reader;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using CsharpHttpHelper;
using CsharpHttpHelper.Enum;
using Dapper;
using DotnetSpider.DataFlow;
using DotnetSpider.DataFlow.Parser;
using DotnetSpider.DataFlow.Storage;
using DotnetSpider.Http;
using DotnetSpider.RequestSupplier;
using DotnetSpider.Scheduler.Component;
using DotnetSpider.Selector;
using DotnetSpider.Statistics.Store;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MongoDB.Bson;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RabbitMQ.Client;
using Serilog;
using StringContent = DotnetSpider.Http.StringContent;

namespace DotnetSpider.Lawyee.Src
{
	public class FaXin : Spider
	{

		public static string user = ConfigurationManager.AppSettings["user"];
		public static string password = ConfigurationManager.AppSettings["password"];


		public static bool isEmpty = true;
		#region 静态属性及公用方法
		public static bool isSchedulerEmpty = false;

		/// <summary>
		/// 是否启用Redis队列去重及错误的记录
		/// </summary>
		public static bool StartRedisQueue = true;

		/// <summary>
		/// 默认开启从Redis获取Token
		/// </summary>
		public static bool RedisToken = true;

		/// <summary>
		/// 浏览器UserAgent
		/// </summary>					  
		public static string UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36";

		//public static string cookie = @"sajssdk_2015_cross_new_user=1; officeId=78D4781246E411E9B8277CD30ABEA58C; token=eyJhbGciOiJIUzI1NiJ9.eyJvZmZpY2VfaWQiOiI3OEQ0NzgxMjQ2RTQxMUU5QjgyNzdDRDMwQUJFQTU4QyIsImRldmljZVR5cGUiOiJ3ZWIiLCJvZmZpY2VfbmFtZSI6IuWMl-S6rOW4guS6rOW4iO-8iOa3seWcs--8ieW-i-W4iOS6i-WKoeaJgCIsInVzZXJfdHlwZSI6IkEiLCJ1c2VyX2lkIjoiMDYzMEFCMDgyMzAwMTFFQUJEOEE3Q0QzMEFFNDgzMjgiLCJsb2dpblR5cGUiOiIxIiwidXNlcl9uYW1lIjoi5Y-R5Liq5bm_5ZGK5a6eIiwiaXNzIjoiaUxhdy5jb20iLCJleHAiOjE1OTg4MDk1Nzg3NzIsImlhdCI6MTU5NjIxNzU3ODc3Miwib2ZmaWNlVHlwZSI6ImludGVncmF0aW9uIn0.Ksv8cri3sFvt1yRtSV9TECKQvi8DB-hO1xrz7QY-jxI; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%220630AB08230011EABD8A7CD30AE48328%22%2C%22%24device_id%22%3A%22173a5fb1d00231-032416b9c7929c-376b4502-2073600-173a5fb1d0185d%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%A4%BE%E4%BA%A4%E7%BD%91%E7%AB%99%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Fopen.weixin.qq.com%2Fconnect%2Fqrconnect%3Fappid%3Dwx19fe9af64436b614%26scope%3Dsnsapi_login%26redirect_uri%3Dhttps%253A%252F%252Falphalawyer.cn%252Fwechatlogin%252Falphalawyer.cn%252F%2523%252Flogin%252Fwxloginback%26state%3D1596%22%2C%22%24latest_referrer_host%22%3A%22open.weixin.qq.com%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%7D%2C%22first_id%22%3A%22173a5fb1d00231-032416b9c7929c-376b4502-2073600-173a5fb1d0185d%22%7D; SERVERID=c76d9a3ce325ca37447cdafab84026c7|1596217593|1596217579";
		//public static string authorization = @"Token 2f44f29fc35518e68d8eb91a3ef0826582cedbfa";
		//public static string token = @"eyJhbGciOiJIUzI1NiJ9.eyJvZmZpY2VfaWQiOiI3OEQ0NzgxMjQ2RTQxMUU5QjgyNzdDRDMwQUJFQTU4QyIsImRldmljZVR5cGUiOiJ3ZWIiLCJvZmZpY2VfbmFtZSI6IuWMl-S6rOW4guS6rOW4iO-8iOa3seWcs--8ieW-i-W4iOS6i-WKoeaJgCIsInVzZXJfdHlwZSI6IkEiLCJ1c2VyX2lkIjoiMDYzMEFCMDgyMzAwMTFFQUJEOEE3Q0QzMEFFNDgzMjgiLCJsb2dpblR5cGUiOiIxIiwidXNlcl9uYW1lIjoi5Y-R5Liq5bm_5ZGK5a6eIiwiaXNzIjoiaUxhdy5jb20iLCJleHAiOjE1OTg4MDk1Nzg3NzIsImlhdCI6MTU5NjIxNzU3ODc3Miwib2ZmaWNlVHlwZSI6ImludGVncmF0aW9uIn0.Ksv8cri3sFvt1yRtSV9TECKQvi8DB-hO1xrz7QY-jxI";

		public static string cookie = @"a";
		public static string authorization = @"a";
		public static string token = @"a";


		/// <summary>
		/// 单页面最大记录条数
		/// </summary>
		public static int PageSize = 50;
		public static string Listurl = $"https://alphalawyer.cn/ilawregu-search/api/v1/lawregu/lawReguList?order=%E6%95%88%E5%8A%9B%E7%BA%A7%E5%88%AB&pageIndex=@pageIndex&pageSize={FaXin.PageSize}&query=@query";

		/// <summary>
		/// 配置文件对象
		/// </summary>
		public static SpiderOptions Options_;

		/// <summary>
		/// 生成随机数
		/// </summary>
		/// <param name="min"></param>
		/// <param name="max"></param>
		/// <returns></returns>

		public static async Task RunAsync()
		{
			//var builder = Builder.CreateDefaultBuilder<AlphaSpider>();  
			var builder = Builder.CreateDefaultBuilder<FaXin>(options =>
			{
				// 每秒 1 个请求
				options.Speed = 0.3;
				options.password = password;
				options.user = user;
				////最大深度
				//options.Depth = 1;

				//最大的队列并发数
				options.RequestedQueueCount = 1000 * 10 * 10 * 10 * 100;

				// 请求超时
				options.RequestTimeout = 10 * 24 * 60 * 60;

				//尝试次数
				options.RetriedTimes = 9999999;

				//Scheduler持续时间
				options.EmptySleepTime = 60 * 60 * 24 * 1500;

				//爬虫是否使用代理
				//options.UseProxy = true;

				//去除外链
				//options.RemoveOutboundLinks = true; 

			});

			builder.UseSerilog();
			builder.UseQueueDistinctBfsScheduler<HashSetDuplicateRemover>();
			await builder.Build().RunAsync();
		}

		public FaXin(
			IOptions<SpiderOptions> options,
			SpiderServices services,
			ILogger<Spider> logger, IStatisticsStore _statisticsStore) : base(options, services, logger, _statisticsStore)
		{
			Options_ = options.Value;
		}

		#region 未处理的重载

		public override void Dispose()
		{
			base.Dispose();
		}

		public override bool Equals(object obj)
		{
			return base.Equals(obj);
		}

		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		public override Task StartAsync(CancellationToken cancellationToken)
		{
			return base.StartAsync(cancellationToken);
		}

		public override Task StopAsync(CancellationToken cancellationToken)
		{
			return base.StopAsync(cancellationToken);
		}

		public override string ToString()
		{
			return base.ToString();
		}

		protected override Spider AddDataFlow(IDataFlow dataFlow)
		{
			return base.AddDataFlow(dataFlow);
		}

		protected override void AddRequestSupplier(IRequestSupplier requestSupplier)
		{
			base.AddRequestSupplier(requestSupplier);
		}

		protected override void ConfigureRequest(Request request)
		{
			base.ConfigureRequest(request);
		}

		protected override Task ExecuteAsync(CancellationToken stoppingToken)
		{
			return base.ExecuteAsync(stoppingToken);
		}


		protected override (string Id, string Name) GetIdAndName()
		{
			//return base.GetIdAndName();
			return (Guid.NewGuid().ToString(), "alpha");
		}


		#endregion

		#endregion

		protected override async Task InitializeAsync(CancellationToken stoppingToken)
		{
			//过虑处理类
			AddDataFlow(new ListDataParser());
			AddDataFlow(new DetailDataParser());

			AddDataFlow(new SqlServerStorage());   //SQL Server存储类

			//await new TimedBackgroundService(Options_).StartAsync();

			intervalThread(GetSetUserState, 60 * 1000 * 2);


			OnSchedulerEmpty += LawSpider_OnSchedulerEmpty;
			Login();
			ListData();
		}


		public static void GetSetUserState()
		{
			string cookie = string.Empty;
			using (var conn = new SqlConnection(Options_.StorageConnectionString))
			{
				//dt = DataTableToIDataReader(conn.ExecuteReader($"select * from LawOptions where id='"+ user + "' "));
				//if (dt.Rows.Count > 0)
				//{
				//	cookie = dt.Rows[0]["cookie"].ToString();
				//}
				cookie = Options_.cookies;
				if (!string.IsNullOrEmpty(cookie))
				{
					HttpHelper http = new HttpHelper();
					WebHeaderCollection header = new WebHeaderCollection();
					header["Accept-Encoding"] = "gzip, deflate";
					header["Accept-Language"] = "zh-CN,zh;q=0.9";
					header["Connection"] = "keep-alive";
					header["X-Requested-With"] = "XMLHttpRequest";
					header["Origin"] = "http://www.faxin.cn";

					HttpItem item = new HttpItem()
					{
						URL = "http://www.faxin.cn/GetUserState.ashx",//URL     必需项    
						Method = "POST",//URL     可选项 默认为Get   
						PostDataType = PostDataType.String,//默认为字符串，同时支持Byte和文件方法
						PostEncoding = System.Text.Encoding.UTF8,//默认为Default，
						Accept = "*/*",
						ContentType = "application/x-www-form-urlencoded",
						Host = "www.faxin.cn",
						Referer = "http://www.faxin.cn/search/GeneralLawSearch.aspx",
						UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
						Header = header,
						Cookie = cookie,
					};
					HttpResult result = http.GetHtml(item);


					string newcooke = string.Empty;
					string cookie2 = HttpHelper.GetSmallCookie(result.Cookie);
					if (!string.IsNullOrEmpty(cookie2))
					{
						string[] cookies = cookie.Split(';', StringSplitOptions.RemoveEmptyEntries);
						for (int i = 0; i < cookies.Length; i++)
						{
							if (cookies[i].IndexOf("lawapp_web") >= 0)
							{
								cookies[i] = cookie2;
							}
							newcooke += cookies[i] + ";";
						}
						//conn.Execute($"update LawOptions set cookie = '" + newcooke + "' where id = '"+ user + "' ");
						Options_.cookies = newcooke;
					}


					//////
					if (newcooke == "")
					{
						newcooke = cookie;
					}
					header = new WebHeaderCollection();
					header["Accept-Encoding"] = "gzip, deflate";
					header["Accept-Language"] = "zh-CN,zh;q=0.9";
					header["Connection"] = "keep-alive";
					header["X-Requested-With"] = "XMLHttpRequest";
					header["Origin"] = "http://www.faxin.cn";

					item = new HttpItem()
					{
						URL = "http://www.faxin.cn/user/UpdateUserState.ashx?num=" + new Random().Next(0, 100),
						Method = "GET",//URL     可选项 默认为Get   
						PostDataType = PostDataType.String,//默认为字符串，同时支持Byte和文件方法
						PostEncoding = System.Text.Encoding.UTF8,//默认为Default，
						Accept = "*/*",
						ContentType = "application/x-www-form-urlencoded",
						Host = "www.faxin.cn",
						Referer = "http://www.faxin.cn/search/GeneralLawSearch.aspx",
						UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
						Header = header,
						Cookie = newcooke,
					};
					result = http.GetHtml(item);
				}
			}
		}
		public static void intervalThread(Action action, double interval)
		{
			new Thread(() => {

				//自执行一次
				//action(); 

				System.Timers.Timer timerThread = new System.Timers.Timer(interval);
				timerThread.Elapsed +=
				new System.Timers.ElapsedEventHandler((_sender, _e) =>
				{
					action();
				});

				timerThread.AutoReset = true;
				timerThread.Enabled = true;

			}).Start();

		}


		int tim = 0;
		private void LawSpider_OnSchedulerEmpty()
		{
			try
			{
				Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！");
				Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！");
				Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！");
				Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！");
				Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！");
				Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！");
				Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！");
				Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！");
				Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！");
				Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！");
				Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！");
				Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！");
				Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！");
				Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！");
				Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！");
				Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！");
				Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！");
				Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！");
				Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！");
				Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！");
				Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！");
				Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！");
				Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！");
				Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！");
				Thread.Sleep(1000 * 60 * 60 * 20);
				Lawyee.Src.FaXin.RunAsync();
				//if (isSchedulerEmpty)
				//{
				//	tim = 0;
				// 	Lawyee.Src.FaXin.RunAsync();
				//	//ListData(1);
				//	isSchedulerEmpty = false;
				//}
				//else
				//{
				//	tim += 1;
				//	Console.WriteLine("请求为0了，，，，，，，，等待加入请求！！！！！！！！！！！！！！！！！！！！！！！！！！！" + tim.ToString());
				//	Thread.Sleep(1000 * 60 * 30);
				//}
			}
			catch (Exception ex)
			{
				Log.Logger.Error(ex.Message + "," + ex.StackTrace);
				Console.WriteLine(ex.Message + "," + ex.StackTrace);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="startDate"></param>
		/// <param name="totalDays">0代表当天</param>
		/// <returns></returns>
		public static void Login()
		{
			bool logintrue = false;
			try
			{
				//WebProxy webProxy = new WebProxy();
				//webProxy.Address = new Uri(string.Format("{0}:{1}", "http://tps163.kdlapi.com", "15818"));
				//webProxy.Credentials = new NetworkCredential("t19186421608282", "7a85i5lf"); //用户名：t19186421608282  密码：7a85i5lf

				while (!logintrue)
				{
					HttpHelper http = new HttpHelper();
					//创建Httphelper参数对象
					HttpItem item = new HttpItem()
					{
						URL = "http://www.faxin.cn/login.aspx",//URL     必需项    
						Method = "get",//URL     可选项 默认为Get   
						ContentType = "text/html",//返回类型    可选项有默认值
												  //WebProxy = webProxy
					};

					//请求的返回值对象
					HttpResult result = http.GetHtml(item);
					//获取请请求的Html
					string html = result.Html;
					//获取请求的Cookie
					string cookie1 = HttpHelper.GetSmallCookie(result.Cookie);
					if (!string.IsNullOrEmpty(cookie1))
					{
						//Header
						//WebHeaderCollection header = result.Header;
						WebHeaderCollection header = new WebHeaderCollection();
						header["Accept-Encoding"] = "gzip, deflate";
						header["Accept-Language"] = "zh-CN, zh; q=0.9";
						header["Cache-Control"] = "max-age=0";
						header["Origin"] = "http://www.faxin.cn";
						header["Upgrade-Insecure-Requests"] = "1";

						item = new HttpItem()
						{
							URL = "http://www.faxin.cn/login.aspx",//URL     必需项    
							Method = "POST",//URL     可选项 默认为Get   
							PostDataType = PostDataType.String,//默认为字符串，同时支持Byte和文件方法
							PostEncoding = System.Text.Encoding.UTF8,//默认为Default，
							Postdata = "__VIEWSTATE=%2FwEPDwUKLTQ3MzE1NzE2MWQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgIFCmlzUmVtZW1iZXIFC2lzQXV0b0xvZ2luBX5spPlbkbdaOOfsiLY2g0mJe7M%3D&__VIEWSTATEGENERATOR=C2EE9ABB&WebUCHead_Special1%24hiddIsLogin=0&WebUCSearchNoAdvSearch1%24lib=&keyword=&user_name=" + user + "&user_password=" + password,//Post要发送的数据
							Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
							ContentType = "application/x-www-form-urlencoded",
							Host = "www.faxin.cn",
							Referer = "http://www.faxin.cn/login.aspx",
							UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
							Header = header,
							Cookie = cookie1,
							//WebProxy = webProxy
						};

						result = http.GetHtml(item);
						string cookie2 = HttpHelper.GetSmallCookie(result.Cookie);
						if (!string.IsNullOrEmpty(cookie2))
						{
							string cookie3 = HttpHelper.GetMergeCookie(cookie1, cookie2);
							//using (var conn = new SqlConnection(Options_.StorageConnectionString))
							//{
							//	conn.Execute($"update LawOptions set cookie = '"+ cookie3 + "' where id='" + user + "' ");
							//}
							Options_.cookies = cookie3;
							logintrue = true;
						}
						else
						{
							//throw new Exception("Login cookie2 null");
						}
					}
				}

				

			}
			catch (Exception ex)
			{
				Log.Logger.Error("Login，" + ex.Message + ex.StackTrace + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
				Console.WriteLine("Login，" + ex.Message + ex.StackTrace + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
				throw ex;
			}

		}

		public static void changelogin()
		{
			//int userID = Convert.ToInt32(Options_.user.Replace("wjhmiss", ""));
			//userID = userID + 1;
			//if (userID > 25)
			//{
			//	userID = 24;
			//}

			//Options_.user = "wjhmiss" + userID.ToString();
			//user = "wjhmiss" + userID.ToString();
			Log.Logger.Error("changelogin，" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));

			Console.WriteLine("changelogin，" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
			Thread.Sleep(5000);
			Login();
		}

		public async void ListData(int count = 0)
		{
			try
			{
				////   http://www.faxin.cn/lib/zyfl/ZyflContent.aspx?gid=A290592&libid=all    http://www.faxin.cn/lib/zyfl/ZyflContent.aspx?gid=A292763&libid=all
				//List<Request> listRequestt = new List<Request>();
				//Request detailrequest = DetailDataParser.ConditionRequst("http://www.faxin.cn/lib/zyfl/ZyflContent.aspx?gid=A290592&libid=all", "010101", "gjfl", "A292763");
				//listRequestt.Add(detailrequest);
				//await AddRequestsAsync((IEnumerable<Request>)listRequestt.ToArray());
				//return;

				List<Request> listRequest = new List<Request>();
				string url = string.Empty;
				string type = string.Empty;
				string postdatas = string.Empty;
				string subtype = string.Empty;
				////"国家法律";  gjfl
				////全部all  法律立法解释010101   司法解释010102   行政法规010103   部门规章010104   其他文件010105

				url = "http://www.faxin.cn/lib/zyfl/GetZyflData.ashx";
				type = "gjfl";
				postdatas = "keyTitle=&keyContent=&fdep_id=&fwzh=&pdep_id=&sort_id=&xiaoli_id=undefined&Fdate_b=&Fdate_e=&Pdate_b=&Pdate_e=&shixiao_id=&Sdate_b=&Sdate_e=&searchtype=0&showsummary=undefined&ckbInSearch=undefined" +
							"&lib=zyfl&chooseNum=@subtype" +
							"&firstPage=@pageindex&secondPage=@pageindex&thirdPage=@pageindex&fourthPage=@pageindex&fifthPage=@pageindex&sixthPage=@pageindex" +
							"&sort_field=&listnum=50&sort_id_left=&xiaoli_id_left=&shixiao_id_left=&fdep_id_left=&isAdvSearch=1&usersearchtype=1";

				bool gjfl1 = Convert.ToBoolean(ConfigurationManager.AppSettings["gjfl1"]);
				if (gjfl1)
				{
					subtype = "010101";
					Request request11 = ListDataParser.ConditionRequst(url, postdatas, "1", subtype, type);
					listRequest.Add(request11);
				}
				bool gjfl2 = Convert.ToBoolean(ConfigurationManager.AppSettings["gjfl2"]);
				if (gjfl2)
				{
					subtype = "010102";
					Request request12 = ListDataParser.ConditionRequst(url, postdatas, "1", subtype, type);
					listRequest.Add(request12);
				}
				bool gjfl3 = Convert.ToBoolean(ConfigurationManager.AppSettings["gjfl3"]);
				if (gjfl3)
				{
					subtype = "010103";
					Request request13 = ListDataParser.ConditionRequst(url, postdatas, "1", subtype, type);
					listRequest.Add(request13);
				}
				bool gjfl4 = Convert.ToBoolean(ConfigurationManager.AppSettings["gjfl4"]);
				if (gjfl4)
				{
					subtype = "010104";
					Request request14 = ListDataParser.ConditionRequst(url, postdatas, "1", subtype, type);
					listRequest.Add(request14);
				}
				bool gjfl5 = Convert.ToBoolean(ConfigurationManager.AppSettings["gjfl5"]);
				if (gjfl5)
				{
					subtype = "010105";
					Request request15 = ListDataParser.ConditionRequst(url, postdatas, "1", subtype, type);
					listRequest.Add(request15);
					//subtype = "all";
					//Request request16 = ListDataParser.ConditionRequst(url, postdatas, "1", subtype, type);
					//listRequest.Add(request16);
				}



				////////地方法规  http://www.faxin.cn/lib/dffl/GetDfflData.ashx
				////////全部all   地方性法规010201   地方政府规章010202  地方司法文件010203  地方规范性文件010204

				url = "http://www.faxin.cn/lib/dffl/GetDfflData.ashx";
				type = "dffg";  //
				postdatas = "keyTitle=&keyContent=&fdep_id=&fwzh=&pdep_id=&sort_id=&xiaoli_id=undefined&Fdate_b=&Fdate_e=&Pdate_b=&Pdate_e=&shixiao_id=&Sdate_b=&Sdate_e=&searchtype=0&showsummary=undefined&ckbInSearch=undefined" +
							"&lib=dffl&chooseNum=@subtype" +
							"&firstPage=@pageindex&secondPage=@pageindex&thirdPage=@pageindex&fourthPage=@pageindex&fifthPage=@pageindex&sixthPage=@pageindex" +
							"&sort_field=&listnum=50&sort_id_left=&shixiao_id_left=&fdep_id_left=&isAdvSearch=1&usersearchtype=1";
				bool dffg1 = Convert.ToBoolean(ConfigurationManager.AppSettings["dffg1"]);
				if (dffg1)
				{
					subtype = "010201";
					Request request21 = ListDataParser.ConditionRequst(url, postdatas, "1", subtype, type);
					listRequest.Add(request21);
				}
				bool dffg2 = Convert.ToBoolean(ConfigurationManager.AppSettings["dffg2"]);
				if (dffg2)
				{
					subtype = "010202";
					Request request22 = ListDataParser.ConditionRequst(url, postdatas, "1", subtype, type);
					listRequest.Add(request22);
				}
				bool dffg3 = Convert.ToBoolean(ConfigurationManager.AppSettings["dffg3"]);
				if (dffg3)
				{
					subtype = "010203";
					Request request23 = ListDataParser.ConditionRequst(url, postdatas, "1", subtype, type);
					listRequest.Add(request23);
				}
				bool dffg4 = Convert.ToBoolean(ConfigurationManager.AppSettings["dffg4"]);
				if (dffg4)
				{
					subtype = "010204";
					Request request24 = ListDataParser.ConditionRequst(url, postdatas, "1", subtype, type);
					listRequest.Add(request24);
				}
				//地区
				bool dffg5 = Convert.ToBoolean(ConfigurationManager.AppSettings["dffg5"]);
				if (dffg5)
				{
					subtype = "010205";
					List<string> area = RetArea();
					foreach (var item in area)
					{
						Request request25 = ListDataParser.ConditionRequst(url, postdatas, "1", subtype, type, item);
						listRequest.Add(request25);
					}
					
				}

				////////立法资料 http://www.faxin.cn/lib/lfsf/GetLfData.ashx
				////////全部all   全国人大立法资料010301   中央行政立法资料010302   地方人大立法资料010303   地方行政立法资料010304   其他010305

				url = "http://www.faxin.cn/lib/lfsf/GetLfData.ashx";
				type = "lfzl";  //
				postdatas = "keyTitle=&keyContent=&fwzh=&fdep_id=&Fdate_b=&Fdate_e=&pdep_id=undefined&sort_id=&searchtype=0&showsummary=undefined&ckbInSearch=undefined" +
							"&lib=lf&chooseNum=@subtype" +
							"&firstPage=@pageindex&secondPage=@pageindex&thirdPage=@pageindex&fourthPage=@pageindex&fifthPage=@pageindex" +
							"&sort_field=&listnum=50&isAdvSearch=1&usersearchtype=1";
				bool lfzl1 = Convert.ToBoolean(ConfigurationManager.AppSettings["lfzl1"]);
				if (lfzl1)
				{
					subtype = "010301"; //             
					Request request31 = ListDataParser.ConditionRequst(url, postdatas, "1", subtype, type);
					listRequest.Add(request31);
				}
				bool lfzl2 = Convert.ToBoolean(ConfigurationManager.AppSettings["lfzl2"]);
				if (lfzl2)
				{
					subtype = "010302"; //             
					Request request32 = ListDataParser.ConditionRequst(url, postdatas, "1", subtype, type);
					listRequest.Add(request32);
				}
				bool lfzl3 = Convert.ToBoolean(ConfigurationManager.AppSettings["lfzl3"]);
				if (lfzl3)
				{
					subtype = "010303"; //             
					Request request33 = ListDataParser.ConditionRequst(url, postdatas, "1", subtype, type);
					listRequest.Add(request33);
				}
				bool lfzl4 = Convert.ToBoolean(ConfigurationManager.AppSettings["lfzl4"]);
				if (lfzl4)
				{
					subtype = "010304"; //             
					Request request34 = ListDataParser.ConditionRequst(url, postdatas, "1", subtype, type);
					listRequest.Add(request34);
				}
				bool lfzl5 = Convert.ToBoolean(ConfigurationManager.AppSettings["lfzl5"]);
				if (lfzl5)
				{
					subtype = "010305"; //             
					Request request35 = ListDataParser.ConditionRequst(url, postdatas, "1", subtype, type);
					listRequest.Add(request35);

				}
				//////////司法资料   http://www.faxin.cn/lib/lfsf/GetLfSfData.ashx
				//////////全部all   两高司法资料010401   地方司法资料010402 

				url = "http://www.faxin.cn/lib/lfsf/GetLfSfData.ashx";
				type = "sfzl";  //
				postdatas = "keyTitle =&keyContent=&fwzh=&fdep_id=&Fdate_b=&Fdate_e=&pdep_id=undefined&sort_id=&searchtype=0&showsummary=undefined&ckbInSearch=undefined" +
							"&lib=lf&chooseNum=@subtype" +
							"&firstPage=@pageindex&secondPage=@pageindex&thirdPage=@pageindex&fourthPage=@pageindex" +
							"&sort_field=&listnum=50&isAdvSearch=1&usersearchtype=1";
				bool sfzl1 = Convert.ToBoolean(ConfigurationManager.AppSettings["sfzl1"]);
				if (sfzl1)
				{
					subtype = "010401"; //             
					Request request41 = ListDataParser.ConditionRequst(url, postdatas, "1", subtype, type);
					listRequest.Add(request41);
				}
				bool sfzl2 = Convert.ToBoolean(ConfigurationManager.AppSettings["sfzl2"]);
				if (sfzl2)
				{
					subtype = "010402"; //             
					Request request42 = ListDataParser.ConditionRequst(url, postdatas, "1", subtype, type);
					listRequest.Add(request42);
				}
				count = count == 0 ? 0 : listRequest.Count;
				await AddRequestsAsync((IEnumerable<Request>)listRequest.ToArray(), count);

			}
			catch (Exception ex)
			{
				Log.Logger.Error("ListData，" + ex.Message + ex.StackTrace + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));

				Console.WriteLine("ListData，" + ex.Message + ex.StackTrace + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
				throw ex;
			}

		}
		public List<string> RetArea()
		{
			List<string> dic = new List<string>();
			dic.Add("001:北京市");
			dic.Add("002:河北省");
			dic.Add("003:天津市");
			dic.Add("004:山西省");
			dic.Add("005:内蒙古自治区");
			dic.Add("006:辽宁省");
			dic.Add("007:吉林省");
			dic.Add("008:黑龙江省");
			dic.Add("009:上海市");
			dic.Add("010:江苏省");
			dic.Add("011:浙江省");
			dic.Add("012:安徽省");
			dic.Add("013:福建省");
			dic.Add("014:江西省");
			dic.Add("015:山东省");
			dic.Add("016:河南省");
			dic.Add("017:湖南省");
			dic.Add("018:湖北省");
			dic.Add("019:广东省");
			dic.Add("020:广西壮族自治区");
			dic.Add("021:四川省");
			dic.Add("022:贵州省");
			dic.Add("023:云南省");
			dic.Add("024:西藏自治区");
			dic.Add("025:陕西省");
			dic.Add("026:甘肃省");
			dic.Add("027:青海省");
			dic.Add("028:宁夏回族自治区");
			dic.Add("029:新疆维吾尔自治区");
			dic.Add("031:海南省");
			dic.Add("032:重庆市");
			return dic;
		}
		public class SqlServerStorage : StorageBase
		{
			public override async Task InitAsync()
			{
			}

			protected override async Task StoreAsync(DataContext context)
			{
				var typeName = typeof(LawRecord).FullName;
				var data = (LawRecord)context.GetData(typeName);
				Console.WriteLine("StorageBase" +  "。1"  + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
				try
				{
					if (data != null)
					{
						using (var conn = new SqlConnection(Options_.StorageConnectionString))
						{
							var result = conn.ExecuteScalar($"select 1 from LawRecord with(updlock,readpast) where Id = @Id", data);

							if (data.subtype == "010205")
							{
								Console.WriteLine("StorageBase" + "。2" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
								if (result == null || result.ToString() != "1")
								{
									conn.Execute(
							   $"INSERT INTO LawRecord ([Id], [content],[GetNavContent],[GetLibLink],[type],[area]) VALUES (@Id,@content,@GetNavContent,@GetLibLink,@type,@area) ",
							   data);
								}
								else
								{
									conn.Execute(
								$"update LawRecord  set [content] =@content  ,[GetNavContent] =@GetNavContent ,[GetLibLink] =@GetLibLink ,[type] =@type ,[area]=@area, [Indexed]=0  where [Id] = @Id ",
								data);
								}
							}
							else
							{
								Console.WriteLine("StorageBase" + "。3" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
								if (result == null || result.ToString() != "1")
								{
									conn.Execute(
								$"INSERT INTO LawRecord ([Id], [content],[GetNavContent],[GetLibLink],[type],[subtype]) VALUES (@Id,@content,@GetNavContent,@GetLibLink,@type,@subtype) ",
								data);
								}
								else
								{
									conn.Execute(
								$"update LawRecord  set [content] =@content  ,[GetNavContent] =@GetNavContent ,[GetLibLink] =@GetLibLink ,[type] =@type ,[subtype] =@subtype , [Indexed]=0 where [Id] = @Id ",
								data);
								}
							}
							
						}
					}
				}
				catch (Exception ex)
				{
					Log.Logger.Error("保存错误，" + ex.Message + ex.StackTrace + data.ToJson() + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));

					Console.WriteLine("保存错误，"+ ex.Message + ex.StackTrace + data.ToJson() + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
					CommonHelper.RetryRequest(context);
					//throw ex;
				}
			}
		}

		public static string ReturnCookie()
		{
			//DataTable dt = new DataTable();

			//using (var conn = new SqlConnection(Options_.StorageConnectionString))
			//{
			//	dt = DataTableToIDataReader(conn.ExecuteReader($"select * from LawOptions where id = '"+ user + "' "));
			//}
			//return dt.Rows[0]["cookie"].ToString();
			return Options_.cookies;
		}

		public static DataTable DataTableToIDataReader(IDataReader reader)
		{
			DataTable objDataTable = new DataTable("Table");
			int intFieldCount = reader.FieldCount;
			for (int intCounter = 0; intCounter < intFieldCount; ++intCounter)
			{
				objDataTable.Columns.Add(reader.GetName(intCounter).ToUpper(), reader.GetFieldType(intCounter));
			}
			objDataTable.BeginLoadData();
			object[] objValues = new object[intFieldCount];
			while (reader.Read())
			{
				reader.GetValues(objValues);
				objDataTable.LoadDataRow(objValues, true);
			}
			reader.Close();
			objDataTable.EndLoadData();
			return objDataTable;
		}

	}

	public class ListDataParser : DataParser
	{
		
		public static Request ConditionRequst(string url, string postdatas ,string pageindex,string subtype,string type,string area = null)
		{
			Request request = null;
			try
			{
				if (url.IndexOf("?") > 0)
				{
					url = url.Substring(0, url.IndexOf("?"));
				}
				request = new Request(url + "?n=" + new Random().Next(1000, 9999));
				request.Name = "ListDataParser";
				
				request.SetProperty("url", url);
				request.SetProperty("type", type);
				request.SetProperty("subtype", subtype);
				request.SetProperty("pageindex", pageindex);
				request.SetProperty("postdatas", postdatas);

				postdatas = postdatas.Replace("@subtype", subtype).Replace("@pageindex", pageindex);

				request.Accept = "application/json, text/javascript, */*; q=0.01";
				request.SetHeader("Accept-Encoding", "gzip, deflate");
				request.SetHeader("Accept-Language", "zh-CN, zh; q=0.9");
				request.SetHeader("Connection", "keep-alive");
				request.SetHeader("Origin", "http://www.faxin.cn/");
				request.SetHeader("Host", "www.faxin.cn");
				request.Referer = "http://www.faxin.cn/lib/zyfl/ZyflSearch.aspx?isAdvSearch=1&keyTitle_gjfl=&keyContent_gjfl=&fdep_id_gjfl=&fdep_text_gjfl=&fwzh_gjfl=&Fdate_b_gjfl=&Fdate_e_gjfl=&Sdate_b_gjfl=&Sdate_e_gjfl=&sort_id_gjfl=&sort_text_gjfl=&shixiao_id_gjfl=&shixiao_text_gjfl=&xiaoli_id_gjfl=&xiaoli_text_gjfl=";
				request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36";
				request.SetHeader("X-Requested-With", "XMLHttpRequest");
				request.Method = "POST";
				//request.SetHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
				if (subtype == "010205" && area != null)
				{
					request.SetProperty("area", area);
					request.Cookie = "CookieKey_AreaId=" + area.Split(':')[0] + ";CookieKey_AreaName=" + HttpUtility.UrlEncode(area.Split(':')[1], Encoding.UTF8).Replace("+", "%20") + ";";
				}
				//设置formdata
				if (!string.IsNullOrEmpty(postdatas))
				{
					RequestContent content = new StringContent(postdatas, "application/x-www-form-urlencoded");
					request.SetContent(content);
				}


			}
			catch (Exception ex)
			{
				Log.Logger.Error("ListDataParser ConditionRequst，" + ex.Message + ex.StackTrace + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));

				Console.WriteLine("ListDataParser ConditionRequst，" + ex.Message + ex.StackTrace + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
				throw ex;
			}

			return request;
		}

		public ListDataParser()
		{
			AddRequiredValidatorEx("ListDataParser");
		}

		protected override Task Parse(DataContext context)
		{
			var result = context.Response.ReadAsString().TrimStart();
			try
			{
				//左边菜单json
				if (string.IsNullOrEmpty(result) || !result.StartsWith("{") || !result.EndsWith("}"))
				{
					Console.WriteLine("ListDataParser" + "。1" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));

					Request request = context.Request.Clone();
					context.AddFollowRequests(request);
					return Task.CompletedTask;
				}
				else
				{

					string type = context.Request.GetProperty("type");
					string subtype = context.Request.GetProperty("subtype");
					string pageindex = context.Request.GetProperty("pageindex");
					string postdatas = context.Request.GetProperty("postdatas");
					string url = context.Request.GetProperty("url");
					string area = context.Request.GetProperty("area");


					//test
					//Request detailrequesttest = DetailDataParser.ConditionRequst("http://www.faxin.cn/lib/zyfl/ZyflContent.aspx?gid=A195396&libid=all", subtype, type, "A195396");
					//context.AddFollowRequests(detailrequesttest);
					///

					JObject jdoc = JObject.Parse(result);
					string contents = string.Empty;
					//FirstHtml   SecondHtml   ThirdHtml   FourthHtml   FifthHtml    SixthHtml
					if (jdoc["FirstHtml"].ToString().Replace(" ", "") != "")
					{
						contents = jdoc["FirstHtml"].ToString();
					}
					else if (jdoc["SecondHtml"].ToString().Replace(" ", "") != "")
					{
						contents = jdoc["SecondHtml"].ToString();
					}
					else if (jdoc["ThirdHtml"].ToString().Replace(" ", "") != "")
					{
						contents = jdoc["ThirdHtml"].ToString();
					}
					else if (jdoc["FourthHtml"].ToString().Replace(" ", "") != "")
					{
						contents = jdoc["FourthHtml"].ToString();
					}
					else if (jdoc["FifthHtml"].ToString().Replace(" ", "") != "")
					{
						contents = jdoc["FifthHtml"].ToString();
					}
					else if (jdoc["SixthHtml"].ToString().Replace(" ", "") != "")
					{
						contents = jdoc["SixthHtml"].ToString();
					}
					else
					{
						Request request = context.Request.Clone();
						context.AddFollowRequests(request);
						return Task.CompletedTask;
					}

					ISelectable Selectable = new HtmlSelectable("<html><head></head><body><ul>" + contents + "</ul></body></html>");
					using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDate"]))
					{
						if (type.IndexOf("gjfl") >= 0)
						{
							Console.WriteLine("ListDataParser" + "。2" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));

							if (Selectable.SelectList(Selectors.XPath(".//div[@class='fz-title1']")) != null)
							{
								var Lists = Selectable.SelectList(Selectors.XPath(".//div[@class='fz-title1']")).ToList();
								foreach (var item in Lists)
								{
									//http://www.faxin.cn/lib/zyfl/ZyflContent.aspx?gid=A195396&libid=all
									//                             ZyflContent.aspx?gid=A195396&libid=010101
									string href = item.Select(Selectors.XPath(".//a/@href"))?.Value;//<a href="ZyflContent.aspx?gid=A272708&libid=010101"
									string Id = href.Substring(href.ToLower().IndexOf("gid=") + 4, href.ToLower().IndexOf("&libid=") - href.ToLower().IndexOf("gid=") - 4);

									try
									{
										var r = conn.ExecuteScalar(" select 1 from LawRecord  with(updlock,readpast)  where Id = @Id ", new { Id = Id });
										if (r == null || r.ToString() != "1")
										//if (true)
										{
											string detailurl = string.Empty;
											if (href.ToLower().IndexOf("http") >= 0)
											{
												detailurl = href;// item.Select(Selectors.XPath(".//h3[@class='w_lt']/a/@href"))?.Value;

											}
											else
											{
												detailurl = "http://www.faxin.cn/lib/zyfl/" + href;// item.Select(Selectors.XPath(".//h3[@class='w_lt']/a/@href"))?.Value;
											}

											Request detailrequest = DetailDataParser.ConditionRequst(detailurl, subtype, type, Id);
											context.AddFollowRequests(detailrequest);

										}
										else
										{
											Console.WriteLine("重复:" + Id);
										}
									}
									catch (Exception es)
									{
										Console.WriteLine("ListDataParser gjfl，" + es.Message + es.StackTrace + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
										throw es;
									}
								}
							}
						}
						if (type.IndexOf("dffg") >= 0)
						{
							Console.WriteLine("ListDataParser" + "。3" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
							if (Selectable.SelectList(Selectors.XPath(".//div[@class='fz-title1']")) != null)
							{
								var Lists = Selectable.SelectList(Selectors.XPath(".//div[@class='fz-title1']")).ToList();
								foreach (var item in Lists)
								{
									//http://www.faxin.cn/lib/dffl/dfflContent.aspx?gid=B1124108&libid=010201
									//                             dfflContent.aspx?gid=B1126774&libid=010201
									string href = item.Select(Selectors.XPath(".//a/@href"))?.Value;//<a href=\"dfflContent.aspx?gid=B1126774&libid=010201\" 
									string Id = href.Substring(href.ToLower().IndexOf("gid=") + 4, href.ToLower().IndexOf("&libid=") - href.ToLower().IndexOf("gid=") - 4);

									try
									{
										var r = conn.ExecuteScalar(" select case when Area is null or Area = '' then 0 else 1 end  from LawRecord  with(updlock,readpast)  where Id = @Id ", new { Id = Id });
										if (r == null || r.ToString() == "0")
										{
											string detailurl = string.Empty;
											if (href.ToLower().IndexOf("http") >= 0)
											{
												detailurl = href;// item.Select(Selectors.XPath(".//h3[@class='w_lt']/a/@href"))?.Value;

											}
											else
											{
												detailurl = "http://www.faxin.cn/lib/dffl/" + href;// item.Select(Selectors.XPath(".//h3[@class='w_lt']/a/@href"))?.Value;
											}

											//Request detailrequest = DetailDataParser.ConditionRequst("http://www.faxin.cn/lib/dffl/dfflContent.aspx?gid=B1109260&libid=010205", subtype, type, "B1109260");
											Request detailrequest = DetailDataParser.ConditionRequst(detailurl, subtype, type, Id, area);
											context.AddFollowRequests(detailrequest);
										}
										else
										{
											Console.WriteLine("重复:" + Id);
										}
									}
									catch (Exception es)
									{
										Console.WriteLine("ListDataParser dffg，" + es.Message + es.StackTrace + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
										throw es;
									}
								}
							}
						}
						if (type.IndexOf("lfzl") >= 0)
						{
							Console.WriteLine("ListDataParser" + "。4" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
							if (Selectable.SelectList(Selectors.XPath(".//div[@class='fz-title1']")) != null)
							{
								var Lists = Selectable.SelectList(Selectors.XPath(".//div[@class='fz-title1']")).ToList();
								foreach (var item in Lists)
								{
									//http://www.faxin.cn/lib/lfsf/LfContent.aspx?gid=G22949&libid=010301
									//                             LfContent.aspx?gid=G22935&libid=010301
									string href = item.Select(Selectors.XPath(".//a/@href"))?.Value;//<a href=\"LfContent.aspx?gid=G22935&libid=010301\"
									string Id = href.Substring(href.ToLower().IndexOf("gid=") + 4, href.ToLower().IndexOf("&libid=") - href.ToLower().IndexOf("gid=") - 4);

									try
									{
										var r = conn.ExecuteScalar(" select 1 from LawRecord  with(updlock,readpast)  where Id = @Id ", new { Id = Id });
										if (r == null || r.ToString() != "1")
										{
											string detailurl = string.Empty;
											if (href.ToLower().IndexOf("http") >= 0)
											{
												detailurl = href;// item.Select(Selectors.XPath(".//h3[@class='w_lt']/a/@href"))?.Value;

											}
											else
											{
												detailurl = "http://www.faxin.cn/lib/lfsf/" + href;// item.Select(Selectors.XPath(".//h3[@class='w_lt']/a/@href"))?.Value;
											}

											Request detailrequest = DetailDataParser.ConditionRequst(detailurl, subtype, type, Id);
											context.AddFollowRequests(detailrequest);
										}
										else
										{
											Console.WriteLine("重复:" + Id);
										}


									}
									catch (Exception es)
									{
										Console.WriteLine("ListDataParser lfzl，" + es.Message + es.StackTrace + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));

										throw es;
									}
								}
							}
						}
						if (type.IndexOf("sfzl") >= 0)
						{
							Console.WriteLine("ListDataParser" + "。5" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
							if (Selectable.SelectList(Selectors.XPath(".//div[@class='fz-title1']")) != null)
							{
								var Lists = Selectable.SelectList(Selectors.XPath(".//div[@class='fz-title1']")).ToList();
								foreach (var item in Lists)
								{
									//http://www.faxin.cn/lib/lfsf/SfContent.aspx?gid=H7007&libid=010402
									//                             SfContent.aspx?gid=H6979&libid=010402
									string href = item.Select(Selectors.XPath(".//a/@href"))?.Value;//<a href=\"SfContent.aspx?gid=H6979&libid=010402\" 
									string Id = href.Substring(href.ToLower().IndexOf("gid=") + 4, href.ToLower().IndexOf("&libid=") - href.ToLower().IndexOf("gid=") - 4);

									try
									{
										var r = conn.ExecuteScalar(" select 1 from LawRecord  with(updlock,readpast)  where Id = @Id ", new { Id = Id });
										if (r == null || r.ToString() != "1")
										{
											string detailurl = string.Empty;
											if (href.ToLower().IndexOf("http") >= 0)
											{
												detailurl = href;// item.Select(Selectors.XPath(".//h3[@class='w_lt']/a/@href"))?.Value;

											}
											else
											{
												detailurl = "http://www.faxin.cn/lib/lfsf/" + href;// item.Select(Selectors.XPath(".//h3[@class='w_lt']/a/@href"))?.Value;
											}

											Request detailrequest = DetailDataParser.ConditionRequst(detailurl, subtype, type, Id);
											context.AddFollowRequests(detailrequest);
										}
										else
										{
											Console.WriteLine("重复:" + Id);
										}
									}
									catch (Exception es)
									{
										Console.WriteLine("ListDataParser sfzl，" + es.Message + es.StackTrace + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
										
										throw es;
									}
								}
							}
						}
					}

					if (pageindex == "1")
					{
						if (type.IndexOf("gjfl") >= 0)
						{
							Console.WriteLine("ListDataParser" + "。6" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
							if (Selectable.SelectList(Selectors.XPath(".//div[@class='pager']")) != null)
							{
								var pager = Selectable.Select(Selectors.XPath(".//div[@class='pager']"));
								string pagertotal = pager.Select(Selectors.XPath(".//span[@class='fl s1']"))?.Value;//<a href="ZyflContent.aspx?gid=A272708&libid=010101"
								int total = Convert.ToInt32(pagertotal.Replace(" ", "").Replace("共", "").Replace("页", ""));
								int spiderPagegjfl = Convert.ToInt32(ConfigurationManager.AppSettings["spiderPagegjfl"]);
								total = spiderPagegjfl < total && spiderPagegjfl > 0 ? spiderPagegjfl : total;
								for (int i = 2; i <= total; i++)
								{
									Request request = ListDataParser.ConditionRequst(url, postdatas, i.ToString(), subtype, type);
									context.AddFollowRequests(request);
								}
							}
						}
						if (type.IndexOf("dffg") >= 0)
						{
							Console.WriteLine("ListDataParser" + "。7" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
							if (Selectable.SelectList(Selectors.XPath(".//div[@class='pager']")) != null)
							{
								var pager = Selectable.Select(Selectors.XPath(".//div[@class='pager']"));
								string pagertotal = pager.Select(Selectors.XPath(".//span[@class='fl s1']"))?.Value;//<a href="ZyflContent.aspx?gid=A272708&libid=010101"
								int total = Convert.ToInt32(pagertotal.Replace(" ", "").Replace("共", "").Replace("页", ""));
								int spiderPage = Convert.ToInt32(ConfigurationManager.AppSettings["spiderPage"]);
								total = spiderPage < total && spiderPage > 0 ? spiderPage : total;
								for (int i = 2; i <= total; i++)
								{
									Request request = ListDataParser.ConditionRequst(url, postdatas, i.ToString(), subtype, type, area);
									context.AddFollowRequests(request);
								}
							}
						}
						if (type.IndexOf("lfzl") >= 0)
						{
							Console.WriteLine("ListDataParser" + "。8" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
							if (Selectable.SelectList(Selectors.XPath(".//div[@class='pager']")) != null)
							{
								var pager = Selectable.Select(Selectors.XPath(".//div[@class='pager']"));
								string pagertotal = pager.Select(Selectors.XPath(".//span[@class='fl s1']"))?.Value;//<a href="ZyflContent.aspx?gid=A272708&libid=010101"
								int total = Convert.ToInt32(pagertotal.Replace(" ", "").Replace("共", "").Replace("页", ""));
								int spiderPage = Convert.ToInt32(ConfigurationManager.AppSettings["spiderPage"]);
								total = spiderPage < total && spiderPage > 0 ? spiderPage : total;
								for (int i = 2; i <= total; i++)
								{
									Request request = ListDataParser.ConditionRequst(url, postdatas, i.ToString(), subtype, type);
									context.AddFollowRequests(request);
								}
							}
						}
						if (type.IndexOf("sfzl") >= 0)
						{
							Console.WriteLine("ListDataParser" + "。9" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
							if (Selectable.SelectList(Selectors.XPath(".//div[@class='pager']")) != null)
							{
								var pager = Selectable.Select(Selectors.XPath(".//div[@class='pager']"));
								string pagertotal = pager.Select(Selectors.XPath(".//span[@class='fl s1']"))?.Value;//<a href="ZyflContent.aspx?gid=A272708&libid=010101"
								int total = Convert.ToInt32(pagertotal.Replace(" ", "").Replace("共", "").Replace("页", ""));
								int spiderPage = Convert.ToInt32(ConfigurationManager.AppSettings["spiderPage"]);
								total = spiderPage < total && spiderPage > 0 ? spiderPage : total;
								for (int i = 2; i <= total; i++)
								{
									Request request = ListDataParser.ConditionRequst(url, postdatas, i.ToString(), subtype, type);
									context.AddFollowRequests(request);
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				Request request = context.Request.Clone();
				context.AddFollowRequests(request);
				Log.Logger.Error("ListDataParser Parse，" + result + ex.Message + ex.StackTrace + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
				Console.WriteLine("ListDataParser Parse，" + result + ex.Message + ex.StackTrace + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
			}

			return Task.CompletedTask;

		}
	}


	public class DetailDataParser : DataParser
	{
		public static Request ConditionRequst(string url, string subtype, string type, string Id,string area = null)
		{
			Request request = null;
			try
			{
				request = new Request(url);
				request.Name = "DetailDataParser";

				request.SetProperty("Id", Id);
				request.SetProperty("url", url);
				request.SetProperty("type", type);
				request.SetProperty("subtype", subtype);
				if (area != null)
				{
					request.SetProperty("area", area);
				}
				request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3";
				request.SetHeader("Accept-Encoding", "gzip, deflate");
				request.SetHeader("Accept-Language", "zh-CN, zh; q=0.9");
				request.SetHeader("Cache-Control", "max-age=0");
				request.SetHeader("Connection", "keep-alive");
				request.SetHeader("Origin", "http://www.faxin.cn/");
				request.SetHeader("Host", "www.faxin.cn");
				request.Referer = "http://www.faxin.cn/lib/zyfl/ZyflSearch.aspx?isAdvSearch=1&keyTitle_gjfl=&keyContent_gjfl=&fdep_id_gjfl=&fdep_text_gjfl=&fwzh_gjfl=&Fdate_b_gjfl=&Fdate_e_gjfl=&Sdate_b_gjfl=&Sdate_e_gjfl=&sort_id_gjfl=&sort_text_gjfl=&shixiao_id_gjfl=&shixiao_text_gjfl=&xiaoli_id_gjfl=&xiaoli_text_gjfl=";
				request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36";
				request.SetHeader("Upgrade-Insecure-Requests", "1");
				request.Method = "GET";

			}
			catch (Exception ex)
			{
				Console.WriteLine("DetailDataParser ConditionRequst，" + ex.Message + ex.StackTrace + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
				Log.Logger.Error("DetailDataParser ConditionRequst，" + ex.Message + ex.StackTrace + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));

				throw ex;
			}

			return request;
		}


		public DetailDataParser()
		{
			AddRequiredValidatorEx("DetailDataParser");
		}

		protected override Task Parse(DataContext context)
		{
			string Id = context.Request.GetProperty("Id");
			string type = context.Request.GetProperty("type");
			string subtype = context.Request.GetProperty("subtype");
			string url = context.Request.GetProperty("url");
			string area = context.Request.GetProperty("area");
			var result = context.Response.ReadAsString().TrimStart();
			try
			{
				//result = "访问频率过快";   //test
				if (string.IsNullOrEmpty(result))
				{
					Console.WriteLine("DetailDataParser" + "。2 : " + Id + " :" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
					CommonHelper.RetryRequest(context);
					return Task.CompletedTask;
				}
				
				else if (result.IndexOf("访问频率过快") >= 0 || result.IndexOf("超过最大限制数") >= 0)
				{
					Console.WriteLine("访问频率过快" + "。3 : " + Id + " :" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
					throw new Exception("访问频率过快");
				}
				else if (result.Replace(" ","").IndexOf("law-title") < 0 || result.IndexOf("您当前的权限无法查看该页面的内容") >= 0 || result.ToLower().IndexOf("timed out") >= 0|| result.ToLower().IndexOf("您没有当前库的访问权限，目前不能继续访问") >= 0)
				{
					Console.WriteLine("您当前的权限无法查看该页面的内容" + "。4 : " + Id + " :" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
					throw new Exception("错误信息");
				}
				else if (result.IndexOf("wjhmiss") < 0)
				{
					Console.WriteLine("未登录wjhmiss" + "。6 : " + Id + " :" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
					throw new Exception("未登录");
				}
				else if (result.IndexOf("访问权限已经") >= 0)
				{
					Console.WriteLine("访问权限已经" + "。6 : " + Id + " :" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
					throw new Exception("访问权限");

				}
				//class='law-title'
				else
				{
					Console.WriteLine("DetailDataParser" + "。5 : " + Id + " :" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
					LawRecord lawrecord = new LawRecord();
					lawrecord.Id = Id;
					lawrecord.type = type;
					lawrecord.subtype = subtype;
					lawrecord.content = result;
					if (!string.IsNullOrEmpty(area))
					{
						lawrecord.area = area.Split(':')[1];
					}

					using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDate"]))
					{
						if (type.IndexOf("gjfl") >= 0)
						{
							//Thread.Sleep(1000);
							if (result.IndexOf("</table>") >= 0 || result.IndexOf("</TABLE>") >= 0)
							{
								string typeName = typeof(LawRecord).FullName;
								context.AddData(typeName, lawrecord);
							}
							else
							{
								Console.WriteLine("DetailDataParser" + "。6 : " + Id + " :" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
								WebHeaderCollection header = new WebHeaderCollection();
								header["Accept-Encoding"] = "gzip, deflate";
								header["Accept-Language"] = "zh-CN, zh; q=0.9";
								header["Connection"] = "keep-alive";
								header["Origin"] = "http://www.faxin.cn";
								header["X-Requested-With"] = "XMLHttpRequest";

								HttpHelper http = new HttpHelper();

								WebProxy webProxy = new WebProxy();
								webProxy.Address = new Uri(string.Format("{0}:{1}", "http://tps163.kdlapi.com", "15818"));
								webProxy.Credentials = new NetworkCredential("t19186421608282", "7a85i5lf"); //用户名：t19186421608282  密码：7a85i5lf


																											 //创建Httphelper参数对象
								HttpItem item = new HttpItem()
								{
									Method = "POST",//URL     可选项 默认为Get   
									Accept = "*/*",
									ContentType = "application/x-www-form-urlencoded",
									Host = "www.faxin.cn",
									Referer = url,
									UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
									Header = header,
									Cookie = FaXin.ReturnCookie() + ";isAutoLogin=off;",
									//WebProxy = webProxy
								};


								ISelectable Selectable = new HtmlSelectable(result);
								//法律关联法条 <img style="padding:0 5px;" onclick="getLaw('A234859',12);" src="../../staticelem/images/flglft-icon.png" alt="法律关联法条" title="法律关联法条" width="11px" height="13px">
								if (Selectable.SelectList(Selectors.XPath(".//span[@class='dt']")) != null)
								{
									Console.WriteLine("DetailDataParser" + "。7 : " + Id + " :" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
									string typ = "法律关联法条";
									item.URL = "http://www.faxin.cn/lib/zyfl/get_tiao_link.ashx";
									var Lists = Selectable.SelectList(Selectors.XPath(".//span[@class='dt']")).ToList();
									foreach (var it1 in Lists)
									{

										//if (it1.Select(Selectors.XPath(".//img[@alt='法律关联法条']")) != null)
										//{
										string tiao = ((DotnetSpider.Selector.HtmlSelectable)it1)._node.Id.Replace("t_", "");
										var re = conn.ExecuteScalar($"select 1 from LawRecordOther  with(updlock,readpast)  where Pid = @Id and type=@typ and tiao =@tiao ", new { Id, typ, tiao });
										item.PostDataType = PostDataType.String;//默认为字符串，同时支持Byte和文件方法
										item.PostEncoding = System.Text.Encoding.UTF8;//默认为Default，
										item.Postdata = "gid=" + Id + "&tiao_num=" + tiao;//Post要发送的数据
										bool glftok = false;
										while (!glftok)
										{
											try
											{
												HttpResult hr = http.GetHtml(item);
												//Thread.Sleep(1000);
												if (hr.StatusCode == HttpStatusCode.OK)
												{
													string content = hr.Html;

													if (content.IndexOf("获取数据成功") >= 0)
													{
														if (re == null || re.ToString() != "1")
														{
															conn.Execute(
														$"INSERT INTO LawRecordOther (Pid,tiao,[content],type) VALUES (@Id,@tiao,@content,@typ) ", new { Id, tiao, content, typ });
														}
														else
														{
															conn.Execute(
														$"update LawRecordOther set [content] = @content  where Pid=@Id and tiao=@tiao and type=@typ ", new { Id, tiao, content, typ });
														}
														glftok = true;
													}
													else
													{
														Console.WriteLine("法律关联法条：" + item.URL + "，date：" + item.Postdata + "。访问权限已经：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
													 

													}
												}
												else
												{
													Console.WriteLine("法律关联法条：" + item.URL + "，date：" + item.Postdata + "。HttpStatusCode.  not ok" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
												}
											}
											catch (Exception ex)
											{
												Log.Logger.Error("法律关联法条：" + item.URL + "，date：" + item.Postdata + "。ex：" + ex.Message + ex.StackTrace + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));

												Console.WriteLine("法律关联法条：" + item.URL + "，date：" + item.Postdata + "。ex：" + ex.Message + ex.StackTrace + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
											}
										}
									}
								}
								//释义<a target="_blank" href="../twsy/TwsyContent.aspx?gid=A234859&amp;tiao=12" title="释义"><img style="padding:0 5px;" width="12px" height="14px" src="../../staticelem/img/law/File.png" alt="释义"></a>
								if (Selectable.SelectList(Selectors.XPath(".//span[@class='dt']")) != null)
								{
									Console.WriteLine("DetailDataParser" + "。8 : " + Id + " :" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
									string typ = "释义";
									var Lists = Selectable.SelectList(Selectors.XPath(".//span[@class='dt']")).ToList();
									foreach (var it1 in Lists)
									{

										//if (it1.Select(Selectors.XPath(".//img[@alt='释义']")) != null)
										//{

										string tiao = ((DotnetSpider.Selector.HtmlSelectable)it1)._node.Id.Replace("t_", "");
										item.URL = "http://www.faxin.cn/lib/twsy/TwsyContent.aspx?gid=" + Id + "&tiao=" + tiao;
										item.Method = "GET";
										item.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3";
										var re = conn.ExecuteScalar($"select 1 from LawRecordOther  with(updlock,readpast)  where Pid = @Id and type=@typ and tiao =@tiao ", new { Id, typ, tiao });

										bool glftok = false;
										while (!glftok)
										{
											try
											{
												HttpResult hr = http.GetHtml(item);
												//Thread.Sleep(1000);
												if (hr.StatusCode == HttpStatusCode.OK)
												{
													string content = hr.Html;
													if (content.IndexOf("访问频率过快") < 0 && content.IndexOf("law-content") >= 0 && content.IndexOf("访问权限已经") < 0 && content.IndexOf("wjhmiss") >= 0)
													{
														ISelectable Selectables = new HtmlSelectable(content);
														var s = Selectables.Select(Selectors.XPath(".//div[@class='law-content']"));
														content = ((DotnetSpider.Selector.HtmlSelectable)s).InnerHtml;

														if (s != null)
														{
															if (re == null || re.ToString() != "1")
															{
																conn.Execute(
															$"INSERT INTO LawRecordOther (Pid,tiao,[content],type) VALUES (@Id,@tiao,@content,@typ) ", new { Id, tiao, content, typ });
															}
															else
															{
																conn.Execute(
															$"update LawRecordOther set [content] = @content  where Pid=@Id and tiao=@tiao and type=@typ ", new { Id, tiao, content, typ });
															}
															glftok = true;
														}
													}
													else
													{
														Console.WriteLine("释义：" + item.URL + "。law-content：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
													}

												}
												else
												{
													Console.WriteLine("释义：" + item.URL + "。HttpStatusCode.OK：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));

												}
											}
											catch (Exception ex)
											{
												Log.Logger.Error("释义：" + item.URL + "。ex：" + ex.Message + ex.StackTrace + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));

												Console.WriteLine("释义：" + item.URL + "。ex：" + ex.Message + ex.StackTrace + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));

											}
										}
									}
								}
								//沿革信息<a target="_blank" id="yg" tiao="14" onclick="ShowLSYGDiv('A290592','14')" title="沿革信息"><img style="padding:0 5px" id="ygxx14" width="15px" height="10px" src="../../staticelem/img/law/Graph.png" alt="沿革信息"></a>
								if (Selectable.SelectList(Selectors.XPath(".//span[@class='dt']")) != null)
								{
									Console.WriteLine("DetailDataParser" + "。9 : " + Id + " :" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
									string typ = "沿革信息";
									var Lists = Selectable.SelectList(Selectors.XPath(".//span[@class='dt']")).ToList();
									foreach (var it1 in Lists)
									{

										//if (it1.Select(Selectors.XPath(".//img[@alt='沿革信息']")) != null)
										//{

										string tiao = ((DotnetSpider.Selector.HtmlSelectable)it1)._node.Id.Replace("t_", "");

										item.URL = "http://www.faxin.cn/lib/zyfl/history.aspx?G=" + Id + "&T=" + tiao;
										item.Method = "GET";
										item.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3";
										header["Upgrade-Insecure-Requests"] = "1";
										var re = conn.ExecuteScalar($"select 1 from LawRecordOther  with(updlock,readpast)  where Pid = @Id and type=@typ and tiao =@tiao ", new { Id, typ, tiao });

										bool glftok = false;
										while (!glftok)
										{
											try
											{
												HttpResult hr = http.GetHtml(item);
												//Thread.Sleep(1000);
												if (hr.StatusCode == HttpStatusCode.OK)
												{
													string content = hr.Html;
													if (content.IndexOf("访问频率过快") < 0 && content.IndexOf("history.aspx") >= 0 && content.IndexOf("访问权限已经") < 0 )
													{
														ISelectable Selectabley = new HtmlSelectable(content);
														var s = Selectabley.Select(Selectors.XPath(".//div[@id='divInfo']"));
														content = ((DotnetSpider.Selector.HtmlSelectable)s).InnerHtml;

														if (s != null)
														{
															if (re == null || re.ToString() != "1")
															{
																conn.Execute(
															$"INSERT INTO LawRecordOther (Pid,tiao,[content],type) VALUES (@Id,@tiao,@content,@typ) ", new { Id, tiao, content, typ });
															}
															else
															{
																conn.Execute(
															$"update LawRecordOther set [content] = @content  where Pid=@Id and tiao=@tiao and type=@typ ", new { Id, tiao, content, typ });
															}
															glftok = true;

														}
													}
													else
													{
														Console.WriteLine("沿革信息：" + item.URL + "，date：" + item.Postdata + "。history.aspx：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));

													}
												}
												else
												{
													Console.WriteLine("沿革信息：" + item.URL + "，date：" + item.Postdata + "。HttpStatusCode.OK：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));

												}
											}
											catch (Exception ex)
											{
												Console.WriteLine("沿革信息：" + item.URL + "，date：" + item.Postdata + "。ex：" + ex.Message + ex.StackTrace + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
												Log.Logger.Error("沿革信息：" + item.URL + "，date：" + item.Postdata + "。ex：" + ex.Message + ex.StackTrace + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));

											}
										}
									}
								}

								//导航
								item.PostDataType = PostDataType.String;//默认为字符串，同时支持Byte和文件方法
								item.PostEncoding = System.Text.Encoding.UTF8;//默认为Default，
								item.Postdata = "gid=" + Id;//Post要发送的数据
								item.URL = "http://www.faxin.cn/lib/zyfl/GetNavContent.ashx";
								item.Method = "POST";
								item.Accept = "*/*";
								lawrecord.GetNavContent = http.GetHtml(item).Html;
								//相关
								item.PostDataType = PostDataType.String;//默认为字符串，同时支持Byte和文件方法
								item.PostEncoding = System.Text.Encoding.UTF8;//默认为Default，
								item.Postdata = "gid=" + Id;//Post要发送的数据
								item.URL = "http://www.faxin.cn/lib/zyfl/GetLibLink.ashx";
								item.Method = "POST";
								item.Accept = "text/plain, */*; q=0.01";
								lawrecord.GetLibLink = http.GetHtml(item).Html;

								string typeName = typeof(LawRecord).FullName;
								context.AddData(typeName, lawrecord);
							}
							
						}
						if (type.IndexOf("dffg") >= 0)
						{
							string typeName = typeof(LawRecord).FullName;
							context.AddData(typeName, lawrecord);
						}
						if (type.IndexOf("lfzl") >= 0)
						{
							string typeName = typeof(LawRecord).FullName;
							context.AddData(typeName, lawrecord);
						}
						if (type.IndexOf("sfzl") >= 0)
						{
							string typeName = typeof(LawRecord).FullName;
							context.AddData(typeName, lawrecord);
						}
					}

					//JObject jdoc = JObject.Parse(result);
					//if (Convert.ToBoolean(jdoc["isSuccess"]))
					//{
					//	jdoc["data"]["law_regulation_introductions_format"] = "";
					//	LawRecord lawrecord = new LawRecord();
					//	lawrecord.Id = jdoc["data"]["lid"].ToString();
					//	lawrecord.content = JsonConvert.SerializeObject(jdoc);
					//	lawrecord.current = jdoc["data"]["posting_date"].ToString();

					//	string typeName = typeof(LawRecord).FullName;
					//	context.AddData(typeName, lawrecord);
					//}
					//else
					//{
					//	Log.Logger.Error("DetailDataParser:Parse:" + result);
					//	using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDate"]))
					//	{
					//		conn.Execute(
					//		$"INSERT INTO recordLawnoall ( [conditionText],[createtimes]) VALUES ('" + result + "','" + DateTime.Now.ToString() + "') ");
					//	}
					//	if (result.IndexOf("记录") >= 0 || result.IndexOf("频繁") >= 0 || result.IndexOf("超频") >= 0 || result.IndexOf("频率") >= 0)
					//	{
					//		throw new ExitException("超频");
					//	}
					//	CommonHelper.RetryRequest(context);
					//}
				}
			}
			catch (Exception ex)
			{
				
				if (ex.Message.IndexOf("访问频率过快") >= 0 || ex.Message.IndexOf("未登录") >= 0 || ex.Message.IndexOf("访问权限") >= 0)
				{
					FaXin.changelogin();
					//throw new ExitException("访问频率过快");
					Thread.Sleep(30 * 1000);
				}
				else if (ex.Message.IndexOf("错误信息") >= 0)
				{
					//throw new ExitException("访问频率过快");
					FaXin.changelogin();
					Thread.Sleep(5 * 1000);
				}
				else
				{
					using (var conn = new SqlConnection(ConfigurationManager.AppSettings["StorageConnectionStringForDate"]))
					{
						conn.Execute(
						$"INSERT INTO recordLawnoall ( [conditionText],[createtimes]) VALUES ('" + ex.StackTrace + "1163,url:" + url + "','" + DateTime.Now.ToString() + "') ");
					}
				}
				
				CommonHelper.RetryRequest(context);
				Log.Logger.Error("DetailDataParser  Parse： date：" + result + "。ex：" + ex.Message + ex.StackTrace + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
				Console.WriteLine("DetailDataParser  Parse： date：" + result + "。ex：" + ex.Message + ex.StackTrace + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));

			}

			return Task.CompletedTask;

		}
	}
	

	public class LawRecord
	{
		public string Id { get; set; }
		public string area { get; set; }
		
		public string content { get; set; }
		public string current { get; set; }
		public string type { get; set; }
		public string subtype { get; set; }
		public int? Indexed { get; set; } = 0;
		public string GetNavContent { get; set; }
		public string GetLibLink { get; set; }

	}
	public class LawRecordOther
	{
		public string Id { get; set; }
		public string content { get; set; }
		public string Pid { get; set; }
		public string type { get; set; }

	}
	public class Law
	{

		public string credit_line { get; set; } //= jdoc["credit_line"].ToString(); //: "部分小写"
		public string dispatch_authority { get; set; } //= jdoc["dispatch_authority"].ToString(); //: "全国人民代表大会"
		public string document_number { get; set; } //= jdoc["document_number"].ToString(); //: "主席令第80号"
		public string eff_level { get; set; } //= jdoc["eff_level"].ToString(); //eff_level : 类型 法律
		public string effective_date { get; set; } //= jdoc["effective_date"].ToString(); //effective_date : "2017年11月04日" 生效日期
		public string posting_date { get; set; } //= jdoc["posting_date"].ToString(); //posting_date : "2017年11月04日"
		public string effective_range { get; set; } //= jdoc["effective_range"].ToString(); //effective_range : "全国"
		public string title { get; set; } //= jdoc["title"].ToString(); //title : "中华人民共和国刑法（2017修正）"
		public string title_format { get; set; } //= jdoc["title_format"].ToString(); //title_format : "中华人民共和国刑法"
		public string industry_labels { get; set; } //= jdoc["industry_labels"].ToString(); //industry_labels  行业
		public string labels { get; set; } //= jdoc["labels"].ToString(); //labels    标签
		public string province { get; set; } //= jdoc["province"].ToString(); //province : "全国"
		public string time_limited { get; set; } //= jdoc["time_limited"].ToString(); //time_limited : "现行有效"
		public string lawReference { get; set; } //= jdoc["rowkey"].ToString(); //lawReference      相关引用如案例，发条  //后续
		public string top_instroductions_jsons { get; set; } //= jdoc["rowkey"].ToString(); //top_instroductions_jsons   里面明细相关法律关联条数top10   //后续
		public string legislationHistory { get; set; } //= jdoc["rowkey"].ToString(); //legislationHistory  修改历史   //后续
		public List<LawDetail> LawDetails { get; set; }

	}
	public class LawDetail
	{
		public LawDetail()
		{
			Id = Guid.NewGuid().ToString();
		}
		public string Id { get; set; }

		public string LawId { get; set; }

		public string Content { get; set; }

		public string Current { get; set; }

	}



}
